<?php
/**
 * @author GhostWei
 * @date Aug 20, 2012
 */
class controller_admin_picture_topic extends system_user_controller_topic {
	
	public function action_index() {
		$this->_goRoute('picture/topic/list');
		$this->render();
	}
	
	public function action_special() {
		$row = $this->_getRow();
		$this->view->row = $row;
		$this->_crumbs()->addPage(system_nav_page::create($row->getLocale()->getTitle())->setRoute('picture/topic/edit')->setParams(array('id' => $row->getId())));
		$this->_crumbs()->addPage(system_nav_page::create("Special")->setRoute('picture/topic/special')->setParams(array('id' => $row->getId())));

		$id = $row->getId();
		$render = null;
		$lang = $this->user->getLang();
		$this->_batchDelete(new picture_special_topic_concept($lang), 'id', "lang = '$lang' AND topic_id = '{$row->getId()}' AND special_id");
		if($this->getRequest()->isPost() && $this->_post('delete')) {
			try {
				$row->save();
			} catch (Exception $e) {
				
			}
		}
		if($this->getRequest()->isPost() && $this->_post('addSpecial')) {
			$specialList = array();
			$render = 'picture/topic/addSpecial.phtml';
			$this->_crumbs()->addPage(system_nav_page::create($this->language('Add Special'))->setRoute('picture/topic/special')->setParams(array('id' => $row->getId())));
			try {
				system_db::getAdapter()->beginTransaction();
				if($this->_post('addSpecialToTopic')) {
					$specialId =  (array) $this->_post('special_id');
					if(empty($specialId)) {
						throw new picture_exception("No Special selected.");
					}
					$_id = implode(',', $specialId);
					$concept = new picture_special_concept($this->user->getLang());
					$specialList = $concept->fetchAll(array("special.id IN($_id)"));

					$sql = "REPLACE INTO `picture_special_topic`(`lang`, `special_id`, `topic_id`) VALUES";
					$_sql = array();
					foreach($specialId AS $sid) {
						$_sql[] = "('$lang', '$sid', '$id')";
					}
					$sql .= implode(',', $_sql);
					system_db::getAdapter()->query($sql);

					$row->save();
					system_db::getAdapter()->commit();
					$this->view->throwMessage = 'Complete';
				}
			} catch (Exception $e) {
				system_db::getAdapter()->rollBack();
				$this->view->throwException = $e;
			}
			$this->view->specialList = $specialList;
		}
		if($render == null) {
			$this->_specialList($row);
		}
		$this->render($render);
	}
	
	public function action_delete() {
		$row = $this->_getRow();
		$localeList = array();
		$concept = new picture_topic_locale_concept();
		foreach($concept->fetchAll(array('topic_id = ?' => $row->getId())) as $locale) {
			$localeList[$locale->getLang()] = $locale;
		}
		if($this->getRequest()->isPost()) {
			$this->referer = $this->_post('referer');
			try {
				system_db::getAdapter()->beginTransaction();
				$this->_auth();
				$row->delete($this->_post('options'));
				system_db::getAdapter()->commit();
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				system_db::getAdapter()->rollBack();
				$this->view->throwException = $e;
			}
		}
		$this->view->localeList = $localeList;
		$this->view->referer = $this->referer;
		$this->view->row = $row;
		$this->_crumbs()->addPage(system_nav_page::create($row->getLocale()->getTitle())->setRoute('picture/topic/edit')->setParams(array('id' => $row->getId())));
		$this->_crumbs()->addPage(system_nav_page::create("Delete")->setRoute('picture/topic/delete')->setParams(array('id' => $row->getId())));
		$this->render();
	}
	
	public function action_addnew() {
		$row = $this->concept->create();
		$row->setIsActive(1);
		$row->setDisplayOrder(0);
		$row = $this->_saveRow($row);
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('New Topic'))->setRoute('picture/topic/addnew'));
		$this->render('picture/topic/row.phtml');
	}
	
	public function action_edit() {
		$row = $this->_getRow();
		$row = $this->_saveRow($row);
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($row->getLocale()->getTitle())->setRoute('picture/topic/edit')->setParams(array('id' => $row->getId())));
		$this->render('picture/topic/row.phtml');
	}
	
	public function action_list() {
		$adapter = new system_paginator_adapter_concept(new picture_topic_concept($this->user->getLang()));
		$adapter->setCurrentPageNumber(intval($this->_param('page', 1)));
		
		$this->view->order = $order = strtolower($this->_get('order'));
		$_order = array("is_active DESC", "display_order ASC");
		if($order) {
			$this->view->sort = $sort = intval($this->_get('sort'));
			switch($order) {
				case 'date':
					{
						$order = 'lastmodified';
						break;
					}
				case 'special':
					{
						$order = 'locale.stotal';
						break;
					}
				default:
					{
						$sort = !$sort;
						$order = 'display_order';
						break;
					}
			}
			$_sort = !$sort ? 'DESC' : 'ASC';
			$_order = array("$order $_sort");
		}
		$adapter->setLimit(30);
		$adapter->order($_order);
		$list = new system_paginator($adapter);
		
		if($this->getRequest()->isPost()) {
			try {
				$_dorder = (array) $this->_post('_display_order');
				$lang = $this->user->getLang();
				foreach((array) $this->_post('display_order') as $id => $value) {
					if(isset($_dorder[$id]) && $_dorder[$id] == $value) continue;
					system_db::getAdapter()->query("UPDATE `picture_topic` SET `display_order` = '$value' WHERE `id` = '$id'");
					system_db::getAdapter()->query("UPDATE `picture_topic_locale` SET `stotal` = (
							SELECT COUNT(1) FROM `picture_special_topic` WHERE `topic_id` = '$id' AND `lang` = '$lang'
						) WHERE `lang` = '$lang' AND `topic_id` = '$id'");
				}
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		
		$this->view->list = $list;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('List'))->setRoute('picture/topic/list'));
		$this->render();
	}

}
